package com.tian.generator.util;

import java.sql.Types;

/**
 * 数据库参数类型
 */
public class DbJavaType {
    /**
     * 将数据库数据类型转换为Java数据类型
     *
     * @param sqlType 数据库类型值
     * @return
     */
    public static String parseTypeFormSqlType(int sqlType) {
        StringBuilder sb = new StringBuilder();
        switch (sqlType) {
            case Types.BIT:
            case Types.BOOLEAN:
                sb.append("boolean");
                break;
            case Types.TINYINT:
                sb.append("byte");
                break;
            case Types.SMALLINT:
                sb.append("short");
                break;
            case Types.INTEGER:
                sb.append("int");
                break;
            case Types.BIGINT:
                sb.append("long");
                break;
            case Types.REAL:
                sb.append("float");
                break;
            case Types.FLOAT:
            case Types.DOUBLE:
                sb.append("double");
                break;
            case Types.DECIMAL:
            case Types.NUMERIC:
                sb.append("BigDecimal");
                break;
            case Types.VARCHAR:
            case Types.CHAR:
            case Types.NCHAR:
            case Types.NVARCHAR:
            case Types.LONGVARCHAR:
            case Types.LONGNVARCHAR:
                sb.append("String");
                break;
            case Types.DATE:
                sb.append("Date");
                break;
            case Types.TIME:
                sb.append("Time");
                break;
            case Types.TIMESTAMP:
                sb.append("Timestamp");
                break;
            case Types.NCLOB:
            case Types.CLOB:
            case Types.BLOB:
            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
                sb.append("byte[]");
                break;
            case Types.NULL:
            case Types.OTHER:
            case Types.JAVA_OBJECT:
                sb.append("Object");
                break;
            default:
                sb.append("Object");

        }
        return sb.toString();
    }
    /**
     * 将数据库数据类型转换为Java数据类型
     *
     * @param sqlType 数据库类型名称
     * @return
     */
    public static String parseTypeFormSqlType(String sqlType) {
        StringBuilder sb = new StringBuilder();
        switch (sqlType) {
            case "bit":
            case "boolean":
                sb.append("boolean");
                break;
            case "tinyint":
                sb.append("byte");
                break;
            case "smallint":
                sb.append("short");
                break;
            case "int":
            case "integer":
                sb.append("int");
                break;
            case "bigint":
                sb.append("long");
                break;
            case "real":
                sb.append("float");
                break;
            case "float":
            case "double":
                sb.append("double");
                break;
            case "decimal":
            case "numeric":
                sb.append("BigDecimal");
                break;
            case "varchar":
            case "char":
            case "nchar":
            case "nvarchar":
            case "longvarchar":
            case "longnvarchar":
                sb.append("String");
                break;
            case "date":
            case "datetime":
                sb.append("Date");
                break;
            case "time":
                sb.append("Time");
                break;
            case "timestamp":
                sb.append("Timestamp");
                break;
            case "nclob":
            case "clob":
            case "blob":
            case "binary":
            case "varbinary":
            case "longvarbinary":
                sb.append("byte[]");
                break;
            case "null":
            case "other":
            case "java_object":
                sb.append("Object");
                break;
            default:
                sb.append("Object");

        }
        return sb.toString();
    }
}
